package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.CodeBoundMachine;
import com.tbit.uqbike.object.pojo.ColorCode;
import com.tbit.uqbike.object.pojo.vo.BatchOperationMsgVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.ColorCodeService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author: chen
 * @Description: 颜色码
 * @Date: 创建于 11:01 2023/8/31
 */
@RequestMapping("/colorCode")
@RestController
@Validated
public class ColorCodeController extends BaseController {

    @Autowired
    private ColorCodeService colorCodeService;

    /**
     * 新增
     *
     * @param colorCode
     * @return
     */
    @RequestMapping("/add")
    public JsonResult insert(@RequestBody @Validated ColorCode colorCode, HttpServletRequest request) {
        if (StrUtil.isNotEmpty(colorCode.getName()) && colorCode.getName().length() > 50){
            throw new BaseException("颜色码名称过长");
        }else if (StrUtil.isNotEmpty(colorCode.getContent()) && colorCode.getContent().length() > 50){
            throw new BaseException("颜色码内容过长");
        }
        LoginInfo info = getLogin(request);
        colorCode.setAddTime(new Date());
        if (colorCode.getAccountId() == null) {
            colorCode.setAccountId(info.id);
        }
        colorCodeService.save(colorCode);
        return JsonResult.ok();
    }

    /**
     * 查询
     *
     * @param colorCode 颜色码内容
     * @return
     */
    @RequestMapping("/query")
    public JsonResult query(@RequestBody ColorCode colorCode, HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        if (colorCode.getAccountId() == null) {
            colorCode.setAccountId(info.id);
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        if (colorCode.getContent() != null) {
            queryWrapper.like("content", colorCode.getContent());
        }
        queryWrapper.eq("accountId", colorCode.getAccountId());
        List<ColorCode> colorCodes = colorCodeService.list(queryWrapper);
        return JsonResult.ok(colorCodes);
    }

    /**
     * 删除颜色码
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping("/del")
    public JsonResult del(@RequestBody JSONObject jsonObject) {
        Integer codeId = jsonObject.getInteger("codeId");
        Assert.notNull(codeId, "颜色码ID不能为空");
        Integer count = colorCodeService.getByCodeId(codeId);
        if (count > 0) {
            throw new BaseException("该颜色码已绑定设备，请先解绑后再删除");
        }
        colorCodeService.removeById(codeId);
        return JsonResult.ok();
    }

    /**
     * 绑定
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @PostMapping("/bound")
    public JsonResult bound(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        String machineNOs = jsonObject.getString("machineNOs");
        Integer codeId = jsonObject.getInteger("codeId");
        BatchOperationMsgVO<String> msgVO = colorCodeService.add(Arrays.asList(machineNOs.split(",")), codeId, info);
        return JsonResult.ok(msgVO);
    }

    /**
     * 解绑
     *
     * @param jsonObject
     * @param request
     * @return
     */
    @RequestMapping("/unbound")
    public JsonResult unbound(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        LoginInfo info = getLogin(request);
        String machineNOs = jsonObject.getString("machineNOs");
        colorCodeService.del(Arrays.asList(machineNOs.split(",")), info);
        return JsonResult.ok();
    }

    /**
     * 分页查询
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping("/queryBound")
    public JsonResult queryBound(@RequestBody JSONObject jsonObject) {
        Integer accountId = jsonObject.getInteger("accountId");
        String machineNO = jsonObject.getString("machineNO");
        Integer codeId = jsonObject.getInteger("codeId");
        String content = jsonObject.getString("content");
        Integer pageNO = jsonObject.getInteger("pageNO");
        Integer rowCount = jsonObject.getInteger("rowCount");
        Page page = new Page(pageNO, rowCount);
        PageHelperUtil.initPage(page);
        //Assert.notNull(accountId,"区域ID不能为空");
        PageResult<CodeBoundMachine> info = colorCodeService.queryPage(accountId, machineNO, content, codeId, page);
        return JsonResult.ok(info);
    }

}
